# --*-- coding:utf8 --*--
import copy
import json
import os

import xlrd

from mapping.dataNode import dataNode
from tools import conf
from tools.path import cur_path

CURPATH = cur_path()
catagforyear = 1
catagforquarter = 2

indexList = [dict() for i in range(3)]
parentNode = dataNode(None, 0)


def loadsheet(name):
    workbook = xlrd.open_workbook(conf.filepath)
    sheet_object = workbook.sheet_by_name(name)
    return sheet_object


def PieData(index):
    x_data = [[], [], []]
    y_data = [[], [], []]
    for son_one in parentNode.son.keys():
        x_data[0].append(son_one)
        y_data[0].append(parentNode.son[son_one].caseDetail[index])
        for son_two in parentNode.son[son_one].son.keys():
            x_data[1].append(son_two)
            y_data[1].append(parentNode.son[son_one].son[son_two].caseDetail[index])
            for son_three in parentNode.son[son_one].son[son_two].son.keys():
                x_data[2].append(son_three)
                y_data[2].append(parentNode.son[son_one].son[son_two].son[son_three].caseDetail[index])
    res = []
    for i in range(3):
        res.append([list(z) for z in zip(x_data[i], y_data[i])])
    # print(res)
    return res


def sunburstData(index):
    from pyecharts import options as opts
    retData = list()
    for son_one in parentNode.son.keys():
        innerDatatwo = list()
        for son_two in parentNode.son[son_one].son.keys():
            innerDatathree = list()
            for son_three in parentNode.son[son_one].son[son_two].son.keys():
                itemthree = opts.SunburstItem(name=son_three,
                                              value=parentNode.son[son_one].son[son_two].son[son_three].caseDetail[
                                                  index])
                innerDatathree.append(itemthree)
            itemtwo = opts.SunburstItem(name=son_two, value=parentNode.son[son_one].son[son_two].caseDetail[index],
                                        children=innerDatathree)
            innerDatatwo.append(itemtwo)
        item = opts.SunburstItem(name=son_one, value=parentNode.son[son_one].caseDetail[index], children=innerDatatwo)
        retData.append(item)
    return retData


def Dataforjson(index, name):
    retData = dict()
    innerData = dict()
    retData['data'] = list()
    for son_one in parentNode.son.keys():
        innerData['name'] = son_one
        innerData['value'] = parentNode.son[son_one].caseDetail[index]
        innerData['children'] = list()
        innerDatatwo = dict()
        for son_two in parentNode.son[son_one].son.keys():
            innerDatatwo['name'] = son_two
            innerDatatwo['value'] = parentNode.son[son_one].son[son_two].caseDetail[index]
            innerDatatwo['children'] = list()
            innerDatathree = dict()
            for son_three in parentNode.son[son_one].son[son_two].son.keys():
                innerDatathree['name'] = son_three
                innerDatathree['value'] = parentNode.son[son_one].son[son_two].son[son_three].caseDetail[index]
                innerDatatwo['children'].append(copy.deepcopy(innerDatathree))
            innerData['children'].append(copy.deepcopy(innerDatatwo))
        retData['data'].append(copy.deepcopy(innerData))
    print(retData['data'])
    # with open('./jsons/%s.json' % name, mode='w', encoding='utf-8') as fp:
    with open(os.path.join(CURPATH, '%s.json' % name), mode='w', encoding='utf-8') as fp:
        json.dump(retData, fp, ensure_ascii=False)


def BarData(start=None, end=None, catag=catagforyear):
    if catag == catagforyear:  # 年度分类 时间问题有个bug, 最新的需要更新日期
        dataforBar = {str(year): [0] * 7 for year in range(int(start[:4]), int(end[:4]) + 1)}
    else:
        # TODO 季度分类
        pass
    for son_one in parentNode.son.keys():
        for son_two in parentNode.son[son_one].son.keys():
            for son_three in parentNode.son[son_one].son[son_two].son.keys():
                for i, _ in enumerate(parentNode.son[son_one].son[son_two].son[son_three].caseDetail):
                    dataforBar[str(parentNode.son[son_one].son[son_two].son[son_three].end)[:4]][i] += _

    return dataforBar
